package com.easa.controller;

import com.alibaba.fastjson.JSONObject;
import com.easa.entity.Laboratorymsg;
import com.easa.service.LaboratorymsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;

@Controller
@RequestMapping("/laboratory")
public class LaboratoryController {

    @Autowired
    private LaboratorymsgService laboratorymsgService;

    //获取实验室信息（后台管理）
    @RequestMapping(value="getLaboratoryMsg",method= RequestMethod.POST)
    @ResponseBody
    public Object getLaboratoryMsg(HttpServletRequest request) throws ParseException, UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String lname = request.getParameter("lname");
        List<Laboratorymsg> laboratorymsg = laboratorymsgService.getLaboratoryMsg(lname);

        JSONObject js = new JSONObject();
        js.put("lmsg",laboratorymsg);
        return js;
    }

    //获取详细的实验室信息（后台管理）
    @RequestMapping(value="getLaboratoryDetail.do",method= RequestMethod.POST)
    @ResponseBody
    public Object getLaboratoryDetail(HttpServletRequest request) throws ParseException, UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String lidStr = request.getParameter("lid");

        int lid = 0;
        Laboratorymsg lmsg = null;
        try {
            lid = Integer.parseInt(lidStr);
            lmsg = laboratorymsgService.getLaboratoryByLid(lid);
        }catch (Exception e){
            System.out.println("lid转换int异常:"+e+"lid:"+lid);
        }

        JSONObject js = new JSONObject();
        js.put("lmsg",lmsg);
        return js;
    }

    //获取详细的实验室信息（后台管理）
    @RequestMapping(value="updateLm.do",method= RequestMethod.POST)
    @ResponseBody
    public Object updateLaboratory(HttpServletRequest request) throws ParseException, UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String lidStr = request.getParameter("lid");
        String lname = request.getParameter("lname");
        String lintroduce = request.getParameter("lintroduce");
        String lcapacityStr = request.getParameter("lcapacity");
        String lresponsible = request.getParameter("lresponsible");
        String lstatusStr = request.getParameter("lstatus");


        int lid = 0;
        int lstatus = 0;
        int lcapacity = 0;
        Boolean isSuccess =false;
        try {
            lid = Integer.parseInt(lidStr);
            lstatus = Integer.parseInt(lstatusStr);
            lcapacity = Integer.parseInt(lcapacityStr);
            Laboratorymsg laboratorymsg = new Laboratorymsg();
            laboratorymsg.setLid(lid);
            laboratorymsg.setLname(lname);
            laboratorymsg.setLintroduce(lintroduce);
            laboratorymsg.setLstatus(lstatus);
            laboratorymsg.setLresponsible(lresponsible);
            laboratorymsg.setLcapacity(lcapacity);
            isSuccess = laboratorymsgService.updateLaboratory(laboratorymsg);
        }catch (Exception e){
            System.out.println("Str转换int异常");
            isSuccess = false;
        }

        JSONObject js = new JSONObject();
        js.put("isSuccess",isSuccess);
        return js;
    }

    //添加实验室信息（后台管理）
    @RequestMapping(value="addLaboratory.do",method= RequestMethod.POST)
    @ResponseBody
    public Object addLaboratory(HttpServletRequest request) throws ParseException, UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String lname = request.getParameter("lname");
        String lintroduce = request.getParameter("lintroduce");
        String lcapacityStr = request.getParameter("lcapacity");
        String lresponsible = request.getParameter("lresponsible");
        String lstatusStr = request.getParameter("lstatus");

        int lstatus = 0;
        int lcapacity = 0;
        boolean isSuccess =false;
        try {
            lstatus = Integer.parseInt(lstatusStr);
            lcapacity = Integer.parseInt(lcapacityStr);
            Laboratorymsg laboratorymsg = new Laboratorymsg();
            laboratorymsg.setLname(lname);
            laboratorymsg.setLintroduce(lintroduce);
            laboratorymsg.setLstatus(lstatus);
            laboratorymsg.setLresponsible(lresponsible);
            laboratorymsg.setLcapacity(lcapacity);
            isSuccess = laboratorymsgService.addLaboratory(laboratorymsg);
        }catch (Exception e){
            System.out.println("Str转换int异常");
            isSuccess = false;
        }

        JSONObject js = new JSONObject();
        js.put("isSuccess",isSuccess);
        return js;
    }
}
